package cn.xutongxue.gtms.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.http.HttpStatus;
import cn.xutongxue.gtms.base.Errors;
import cn.xutongxue.gtms.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author XuTongXue
 * @version 1.0
 * @date 2023/3/1 19:40
 * @Description
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return Result.fail(Errors.UNKNOWN_ERROR);
    }


    @ExceptionHandler(NotLoginException.class)
    public Result notLoginExceptionHandler(NotLoginException e) {
        log.error("NotLoginException", e);
        return Result.fail(HttpStatus.HTTP_UNAUTHORIZED, e.getMessage());
    }

    // 全局异常拦截
    @ExceptionHandler
    public Result handlerException(Exception e) {
        log.warn("Exception:", e.getMessage());
        return Result.fail(400500, e.getMessage());
    }
}
